package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.dao.MemberDao;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.pojo.Member;
import com.ghh.service.MemberService;
import com.ghh.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        //定义一个list集合，存储会员数
        List<Integer> memberCount = new ArrayList<>();
        //遍历月份
        for (String month : months) {
            //将月份后拼接"-31"，
            String date = month+"-31";
            //根据月份查询会员数
            Integer count = memberDao.getMemberReportByMonth(date);
            //存入集合
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public Map<String, Object> getDayAndMemner() {
        List<Map<String, Object>> list = memberDao.getDayAndMemner();
        Map<String, Object> map = new HashMap<>();
        List<Object> months = new ArrayList<>();
        List<Object> memberCount = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            Set<Map.Entry<String, Object>> entries = stringObjectMap.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                if("months".equals(key)){
                    months.add(entry.getValue());
                }
                if("memberCount".equals(key)){
                    memberCount.add(entry.getValue());
                }
            }
        }
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }

    //分页查询会员信息
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //定义需要的数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id删除会员信息
    @Override
    public void deleteMember(int id) {
        memberDao.deleteMember(id);
    }

    @Override
    public void updateMember(Member member) {
        memberDao.updateMember(member);
    }

    @Override
    public Member findById(int id) {
        return memberDao.findById(id);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }


}
